use html_parser::{form::*, DomNode, HtmlParser, ParserConfig};

fn main() {
    // 示例HTML表单
    let html = r#"
    <!DOCTYPE html>
    <html>
    <head>
        <title>表单测试</title>
    </head>
    <body>
        <h1>用户注册</h1>
        <form id="register-form" action="/register" method="post">
            <div>
                <label for="username">用户名:</label>
                <input type="text" id="username" name="username" required>
            </div>
            
            <div>
                <label for="email">邮箱:</label>
                <input type="email" id="email" name="email" required>
            </div>
            
            <div>
                <label for="password">密码:</label>
                <input type="password" id="password" name="password" required>
            </div>
            
            <div>
                <label>性别:</label>
                <input type="radio" id="male" name="gender" value="male" checked>
                <label for="male">男</label>
                <input type="radio" id="female" name="gender" value="female">
                <label for="female">女</label>
            </div>
            
            <div>
                <label>兴趣爱好:</label>
                <input type="checkbox" id="hobby1" name="hobbies" value="reading">
                <label for="hobby1">阅读</label>
                <input type="checkbox" id="hobby2" name="hobbies" value="sports" checked>
                <label for="hobby2">运动</label>
                <input type="checkbox" id="hobby3" name="hobbies" value="music" checked>
                <label for="hobby3">音乐</label>
            </div>
            
            <div>
                <label for="country">国家:</label>
                <select id="country" name="country">
                    <option value="cn">中国</option>
                    <option value="us" selected>美国</option>
                    <option value="jp">日本</option>
                </select>
            </div>
            
            <div>
                <label for="message">留言:</label>
                <textarea id="message" name="message">默认留言</textarea>
            </div>
            
            <div>
                <label for="avatar">头像:</label>
                <input type="file" id="avatar" name="avatar">
            </div>
            
            <div>
                <button type="submit">注册</button>
                <button type="reset">重置</button>
                <input type="submit" name="submit_alternative" value="另一种提交方式">
            </div>
        </form>
    </body>
    </html>
    "#;

    // 解析HTML
    let parser = HtmlParser::new(ParserConfig::default());
    let dom = parser.parse(html).expect("解析HTML失败");

    // 提取表单
    match parser.extract_forms(&dom) {
        Ok(forms) => {
            println!("找到{}个表单", forms.len());

            for (i, form) in forms.iter().enumerate() {
                println!("表单 #{}:", i + 1);
                println!("  ID: {:?}", form.id);
                println!("  名称: {:?}", form.name);
                println!("  动作: {:?}", form.action);
                println!("  方法: {:?}", form.method);
                println!("  编码类型: {:?}", form.enctype);

                println!("  字段:");
                for (name, value) in &form.fields {
                    println!("    {}: {:?}", name, value);
                }

                println!("  按钮:");
                for button in &form.buttons {
                    println!(
                        "    {:?} - 名称: {:?}, 值: {:?}, 文本: {}",
                        button.button_type, button.name, button.value, button.text
                    );
                }

                // 序列化为URL编码
                println!("\n  URL编码表单数据:");
                println!("    {}", form.to_url_encoded());

                // 序列化为JSON
                if let Ok(json) = form.to_json() {
                    println!("\n  JSON表单数据:");
                    println!("    {}", json);
                }

                // 表单验证
                let required_fields = ["username", "email", "password"];
                match form.validate_required(&required_fields) {
                    Ok(_) => println!("\n  所有必填字段已填写"),
                    Err(missing) => println!("\n  缺少必填字段: {:?}", missing),
                }
            }
        }
        Err(e) => {
            eprintln!("提取表单错误: {:?}", e);
        }
    }

    // 根据ID查找特定表单
    match parser.find_form(&dom, "register-form") {
        Ok(Some(form)) => {
            println!("\n通过ID找到表单 'register-form'");
            println!("方法: {:?}", form.method);
            println!("动作: {:?}", form.action);
        }
        Ok(None) => {
            println!("\n未找到ID为 'register-form' 的表单");
        }
        Err(e) => {
            eprintln!("查找表单错误: {:?}", e);
        }
    }
}
